<?php

namespace app\sys\controller;

use app\common\model\MenuModel;
use think\App;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\Request;

class Menu extends Base
{
    private $menu_model;
    private $permission_model;
    public function __construct(App $app = null) {
        parent::__construct($app);
        $this->menu_model = model('common/MenuModel');
        $this->permission_model = model('common/PermissionModel');
    }

    /**
     * @return \think\response\Json
     * @throws DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function login_menu() {
        $data['rows'] = $this->menu_model->login_menu();
        $data['status'] = true;
        $data['msg'] = '获取菜单成功！！！';
        return json($data);
    }

    /**
     * @param Request $request
     * @return \think\response\Json|\think\response\View
     * @throws DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index(Request $request) {
        if (!$request->isAjax()) {
            return view();
        } else {
            $model = $this->menu_model->where('1', '1');
            $search = $request->param('search');
            if ($search != null && $search != '') {
                $model->where('name|module_name|action_name|function_name', 'like', '%' . $search . '%');
            }
            $model->order('sort');
            $data['rows'] = $model->select();
            return json($data);
        }
    }

    public function add(Request $request) {
        if (!$request->isAjax()) {
            return view();
        } else {
            $menu = new MenuModel(array(
                'name' => $request->param('name'),
                'parent_id' => $request->param('parent_id'),
                'module_name' => $request->param('module_name'),
                'action_name' => $request->param('action_name'),
                'function_name' => $request->param('function_name'),
                'is_menu' => $request->param('is_menu'),
                'sort' => $request->param('sort'),
                'user_id' => session('loginUser')->id,
                'icon' => $request ->param('icon')
            ));
            $menu->save();
            $data['menu'] = $menu;
            $data['status'] = true;
            $data['msg'] = '添加成功';

            return json($data);
        }
    }

    /**
     * @param Request $request
     * @return \think\response\Json|\think\response\View
     * @throws DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit(Request $request) {
        if (!$request->isAjax()) {
            $menu_id = $request->param('id');
            try {
                $menu = $this->menu_model->where('id', '=', $menu_id)->find();
                if ($menu == null) {
                    exit("<script type='text/javascript'>alert('对象不存在或已被删除');window.history.back(-1);</script>");
                }
                $data['menu'] = $menu;
                $parent_menus = $this->menu_model->where('id', '=', $menu->parent_id)->select();
                $data['parent_menu_name'] = '';
                if (count($parent_menus) > 0) {
                    $data['parent_menu_name'] = $parent_menus[0]->name;
                }
                return view('', $data);
            } catch (DataNotFoundException $e) {
                exit("<script type='text/javascript'>alert('对象不存在或已被删除');window.history.back(-1);</script>");
            }
        } else {
            $menu_id = $request->param('id');
            $menu = $this->menu_model->where('id', '=', $menu_id)->find();
            if ($menu == null) {
                return json([
                    'status' => false,
                    'msg' => '对象不存在或已被删除'
                ]);
            }

            $menu['name'] = $request->param('name');
            $menu['parent_id'] = $request->param('parent_id');
            $menu['module_name'] = $request->param('module_name');
            $menu['action_name'] = $request->param('action_name');
            $menu['function_name'] = $request->param('function_name');
            $menu['sort'] = $request->param('sort');
            $menu['is_menu'] = $request->param('is_menu');
            $menu['update_time'] = date('Y-m-d H:i:s', time());
            $menu['icon'] = $request->param('icon');
            $menu->save();
            return json([
                'status' => true,
                'msg' => '修改成功',
                'menu' => $menu
            ]);
        }
    }

    public function delete(Request $request) {
        $ids = $request->param('ids');
        if ($ids == null) {
            return json([
                'status' => false,
                'msg' => '请传递要删除的IDS'
            ]);
        }

        $idsArray = explode('-', $ids);
        Db::transaction(function () use ($idsArray) {
            foreach ($idsArray as $id) {
                $this->permission_model->where('menu_id', '=', $id)->delete();
                $this->menu_model->where('id', '=', $id)->delete();
            }
        });
        return json([
            'status' => true,
            'msg' => '删除成功',
            'ids' => $idsArray
        ]);
    }


    /**
     * @param Request $request
     * @return \think\response\Json
     * @throws DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_menus(Request $request) {
        $parent_id = $request->param('id');
        $model = $this->menu_model->where('1', '1');

        if ($parent_id != null && $parent_id != '') {
            $model->where('parent_id', '=', $parent_id);
        } else {
            $model->where('parent_id', '=', 0);
        }
        $data = $model->select();

        foreach ($data as $key => $val) {
            $count = $this->menu_model->where('parent_id', '=', $val->id)->count();
            if ($count > 0) {
                $data[$key]['has_child'] = true;
            } else {
                $data[$key]['has_child'] = false;
            }
        }
        return json($data);
    }
}
